$(document).ready(function() {
    querySelfInfo();
    layui.use('form', function() {
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(data) {
            let params = {};

            params.realname = data.field.realname;
            params.gender = data.field.gender;
            params.roleId = '';
            params.idNumber = data.field.idNumber;
            params.password = data.field.password;
            params.cellphone = data.field.cellphone;


            editUser(params)
            return false;
        });


    });
})


querySelfInfo = () => {
    layui.use('form', function() {
        var form = layui.form;
        let loginUser = JSON.parse(localStorage.getItem('loginUser'));
        form.val("formTest", {
            "realname": loginUser.username,
            "gender": loginUser.gender == '男' ? 2 : 1,
            "roleId": loginUser.roleId,
            "idNumber": loginUser.idNumber,
            "cellphone": loginUser.cellphone,
        });
    });

}



editUser = (params) => {

    params.userId = JSON.parse(localStorage.getItem('loginUser')).userId;
    $.ajax({
        url: ajaxPrefix() + '/manage/user/update',
        type: 'post',
        data: JSON.stringify(params),
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("SessionId", localStorage.getItem('sessionId'));
        },
        contentType: "application/json",
        success: function(res) {
            if (res.status == 200) {
                layer.msg('修改成功', { time: 1000 });
            } else {
                layer.msg('操作失败！', { time: 1000 })
            }

        }
    });

}